package com.qyhl.webtv.module_microvideo.shortvideo.shoot.upload.select;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoThemeBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_microvideo.R;
import com.qyhl.webtv.module_microvideo.R2;
import com.qyhl.webtv.module_microvideo.shortvideo.theme.ShortVideoThemeContract;
import com.qyhl.webtv.module_microvideo.shortvideo.theme.ShortVideoThemePresenter;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 标签选择
 */
@Route(path = ARouterPathConstant.SHORT_VIDEO_TAG_SELECT)
public class ShortVideoTagSelectActivity extends BaseActivity implements ShortVideoThemeContract.ShortVideoThemeView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private CommonAdapter commonAdapter;
    private List<ShortVideoThemeBean> mDataList = new ArrayList<>();
    private ShortVideoThemePresenter mPresenter;
    @Autowired(name = "id")
    String activityId;

    private ShortVideoThemeBean mBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.micro_activity_short_video_tag_select;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new ShortVideoThemePresenter(this);
        initView();

    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getList(activityId);
    }

    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(false);
        refresh.setEnableRefresh(false);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.setAdapter(commonAdapter = new CommonAdapter<ShortVideoThemeBean>(this, R.layout.micro_item_short_video_select_tag, mDataList) {
            @Override
            protected void convert(ViewHolder holder, ShortVideoThemeBean bean, int position) {
                holder.setText(R.id.title, "#" + bean.getName() + "#");
                holder.setText(R.id.person_num, bean.getParticipateNumber() + "参与");
            }
        });
    }

    private void setListeners() {
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                mBean = mDataList.get(position);
                finishActivity(mBean);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        loadMask.setOnReloadListener(v -> {
            loadMask.setReloadButtonText("加载中...");
            mPresenter.getList(activityId);
        });
    }

    @OnClick({R2.id.back_btn, R2.id.search})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.back_btn) {
            finishActivity(mBean);
        } else if (i == R.id.search) {
            Bundle bundle = new Bundle();
            bundle.putString("id", activityId);
            bundle.putString("type", "tag");
            RouterManager.router(this,ARouterPathConstant.SHORT_VIDEO_SEARCH,bundle,121);


        }
    }

    @Override
    public void setList(List<ShortVideoThemeBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        mDataList.clear();
        mDataList.addAll(beanList);

        commonAdapter.notifyDataSetChanged();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                finishActivity(mBean);
            default:
                break;

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void setError(String msg) {
        loadMask.setReloadButtonText("点击重试~");
        if (NetUtil.isNetworkAvalible(this)) {
            loadMask.setErrorText("暂无主题或列表获取失败，请重新尝试！");
            loadMask.setErrorImage(R.drawable.error_content);
        } else {
            loadMask.setErrorText("网络异常，请重新尝试！");
            loadMask.setErrorImage(R.drawable.error_network);
        }

        loadMask.setStatus(LoadingLayout.Error);
    }

    private void finishActivity(ShortVideoThemeBean bean) {
        Intent intent = new Intent();
        if (bean != null && bean.getName() != null) {
            intent.putExtra("title", bean.getName());
            intent.putExtra("id", bean.getId());
        } else {
            intent.putExtra("title", "");
            intent.putExtra("id", -1);
        }
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
                ShortVideoThemeBean bean = (ShortVideoThemeBean) Objects.requireNonNull(data.getExtras()).getSerializable("bean");
                if (bean != null && bean.getName() != null) {
                    finishActivity(bean);
                }
                break;
            default:
                break;
        }
    }


}
